﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExam
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
           
            string cmdSting = "select * from Articles";
            var dataTable = DbHelper.GetDataTable(cmdSting);  
            dataGridView1.DataSource = dataTable;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql =string.Format("select * from Articles where Title like'%{0}%'",name);
            var data = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = data;
        }

        //更新
        private void button2_Click(object sender, EventArgs e)
        {
            EidtForm eidtForm = new EidtForm();
            var res=eidtForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var cmdStringUse = string.Format("select * from Articles where Title like '%{0}%'", name);
                var dataTable = DbHelper.GetDataTable(cmdStringUse);
                dataGridView1.DataSource = dataTable;
            }
            else
            {
                MessageBox.Show("否No");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                 var id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
                 var title =(string) dataGridView1.SelectedRows[0].Cells[1].Value;
                 var author = (string)dataGridView1.SelectedRows[0].Cells[2].Value;

                 EidtForm eidtForm = new EidtForm(id,title,author);
                 var res=eidtForm.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var name = textBox1.Text;
                    var cmdStringUse = string.Format("select * from Articles where Title like '%{0}%'", name);
                    var dataTable = DbHelper.GetDataTable(cmdStringUse);
                    dataGridView1.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("否No");
                }
            }
            else
            {
                MessageBox.Show("未选中任意数据行", "prompt");
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("是否删除？", "prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete  from Articles where Id ={0}", id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;
                        var cmdStringUse = string.Format("select * from Articles where Title like '%{0}%'",name);
                        var dataTable = DbHelper.GetDataTable(cmdStringUse);
                        dataGridView1.DataSource = dataTable;
                        MessageBox.Show("已删除", "prompt");
                    }
                }               
            }
            else
                {
                    MessageBox.Show("未选中任意行", "prompt");
                }
                
           
            
        }
    }
}
